<script setup>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";
import useUserStore from "@/store/modules/user";

import FaceRecognition from "@/views/admin/utils/components/FaceRecognition";
import RecordedAT from "@/views/admin/utils/components/RecordedAT";

const userStore = useUserStore();
const route = useRoute();
const router = useRouter();
const { proxy } = getCurrentInstance();

const loginForm = ref({
  username: "admin",
  password: "admin123",
  rememberMe: false,
  code: "",
  uuid: "",
});

const loginRules = {
  username: [{ required: true, trigger: "blur", message: "请输入您的账号" }],
  password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
  code: [{ required: true, trigger: "change", message: "请输入验证码" }],
};

const codeUrl = ref("");
const loading = ref(false);
// 验证码开关
const captchaEnabled = ref(true);
// 注册开关
const register = ref(false);
const redirect = ref(undefined);

watch(
  route,
  (newRoute) => {
    redirect.value = newRoute.query && newRoute.query.redirect;
  },
  { immediate: true }
);

function handleLogin() {
  proxy.$refs.loginRef.validate((valid) => {
    if (valid) {
      loading.value = true;
      // 勾选了需要记住密码设置在 cookie 中设置记住用户名和密码
      if (loginForm.value.rememberMe) {
        Cookies.set("username", loginForm.value.username, { expires: 30 });
        Cookies.set("password", encrypt(loginForm.value.password), {
          expires: 30,
        });
        Cookies.set("rememberMe", loginForm.value.rememberMe, { expires: 30 });
      } else {
        // 否则移除
        Cookies.remove("username");
        Cookies.remove("password");
        Cookies.remove("rememberMe");
      }
      // 调用action的登录方法
      userStore
        .login(loginForm.value)
        .then(() => {
          const query = route.query;
          const otherQueryParams = Object.keys(query).reduce((acc, cur) => {
            if (cur !== "redirect") {
              acc[cur] = query[cur];
            }
            return acc;
          }, {});
          router.push({ path: redirect.value || "/", query: otherQueryParams });
        })
        .catch(() => {
          loading.value = false;
          // 重新获取验证码
          if (captchaEnabled.value) {
            getCode();
          }
        });
    }
  });
}

function getCode() {
  getCodeImg().then((res) => {
    captchaEnabled.value =
      res.captchaEnabled === undefined ? true : res.captchaEnabled;
    if (captchaEnabled.value) {
      codeUrl.value = "data:image/gif;base64," + res.img;
      loginForm.value.uuid = res.uuid;
    }
  });
}

function getCookie() {
  const username = Cookies.get("username");
  const password = Cookies.get("password");
  const rememberMe = Cookies.get("rememberMe");
  loginForm.value = {
    username: username === undefined ? loginForm.value.username : username,
    password:
      password === undefined ? loginForm.value.password : decrypt(password),
    rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
  };
}

getCode();
getCookie();


// 导入切换人脸识别的状态组件 

import {useLoginStore, useCounterStore}  from '@/views/admin/store/login.js'
// import {useLoginStore}  from '@/store/modules/login.js'

const store =  useLoginStore()



</script>





<template>
  <div class="main">
    <div class="header">
      <div class="header-center fl">
        <div  @click="increment" class="header-title">星云教育数据中台-{{count}}</div>
        <div class="header-img"></div>
      </div>
    </div>

    <div class="content">
         <div class="content-right">

        <!-- 声纹识别 -->
        <div  class="voicelogin" v-if="store.isvoiceVisible">
          <RecordedAT   ></RecordedAT>
        </div>
        <!-- 人脸登录 -->
        <FaceRecognition  v-if="store.isfaceVisible"></FaceRecognition>

        <!-- 正常登录 -->
        <div class="right-infp"  v-if="store.isformVisible">

          <div class="right-infp-name">
           
           

            <el-form
              ref="loginRef"
              :model="loginForm"
              :rules="loginRules"
              class="login-form"
            >
           
              <el-form-item prop="username">
                <el-input
                  v-model="loginForm.username"
                  type="text"
                  size="large"
                  auto-complete="off"
                  placeholder="账号"
                >
                  <template #prefix
                    ><svg-icon
                      icon-class="user"
                      class="el-input__icon input-icon"
                  /></template>
                </el-input>
              </el-form-item>
              <el-form-item prop="password">
                <el-input
                  v-model="loginForm.password"
                  type="password"
                  size="large"
                  auto-complete="off"
                  placeholder="密码"
                  @keyup.enter="handleLogin"
                >
                  <template #prefix
                    ><svg-icon
                      icon-class="password"
                      class="el-input__icon input-icon"
                  /></template>
                </el-input>
              </el-form-item>
              <el-form-item prop="code" v-if="captchaEnabled">
                <el-input
                  v-model="loginForm.code"
                  size="large"
                  auto-complete="off"
                  placeholder="验证码"
                  style="width: 63%"
                  @keyup.enter="handleLogin"
                >
                  <template #prefix
                    ><svg-icon
                      icon-class="validCode"
                      class="el-input__icon input-icon"
                  /></template>
                </el-input>
                <div class="login-code">
                  <img :src="codeUrl" @click="getCode" class="login-code-img" />
                </div>
              </el-form-item>

              <el-form-item>
                <div class="right-infp-btn">
                  <a @click="store.setfaceState"> 人脸识别登录 </a>
                  <a class="ml10" @click="store.setvoiceState"> 声纹登录 </a>
                </div>
              </el-form-item>

              <el-form-item style="width: 100%">
                <el-button
                  :loading="loading"
                  size="large"
                  type="primary"
                  style="width: 100%"
                  @click.prevent="handleLogin"
                >
                  <span v-if="!loading">登 录</span>
                  <span v-else>登 录 中...</span>
                </el-button>
                <div style="float: right" v-if="register">
                  <router-link class="link-type" :to="'/register'"
                    >立即注册</router-link
                  >
                </div>
              </el-form-item>
            </el-form>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
    

    

<style scoped >
.main {
  font-size: 0.16rem;
  color: #ffffff;
  background: url("@/views/admin/utils/components/FaceRecognition/assets/dengimages/bj.jpg")
    no-repeat;

  background-size: cover;

  width: 100%;
  height: 100%;
  position: relative;
}

/*头部*/
.header {
  margin: 0 auto;
  width: 80%;
  height: 4.5rem;
  /*background: red;*/
  display: flex;
  justify-content: center;
}

.header .header-center {
  width: 50%;
  height: 3.6rem;
  position: relative;
  margin: 0 auto;
}
.header .header-center .header-title {
  transform: scale(1.5);
  text-align: center;
  color: #ffffff;
  font-size: 22px;
  text-shadow: 0 0 0.5rem #79bbff;
  line-height: 3.82rem;
  background-image: -webkit-linear-gradient(
    left,
    #79bbff 50%,
    #79bbff 50%,
    #fff 100%,
    #79bbff
  );
  -webkit-text-fill-color: transparent;
  -webkit-background-clip: text;
  -webkit-background-size: 200% 100%;
  animation: masked-animation 3s infinite linear;
}

@keyframes masked-animation {
  0% {
    background-position: 0 0;
  }

  100% {
    background-position: -100% 0;
  }
}
.header .header-img {
  transform: scale(1.5);
  background: url("./assets/dengimages/head.gif") no-repeat center center;
  background-size: cover;
  height: 100%;
  width: 100%;
  position: absolute;
  top: 2.9rem;
}

.content {
  position: absolute;
  top: 52%;
  left: 50%;

  transform: translate(-50%, -50%);
  /* display: flex;
  justify-content: space-between; */
  width: 30rem;
  height: 30rem;
  /*background: green;*/
  margin: 0 auto;
  /*border: 1px solid green;*/
}
/* .content-left,
.content-right {
  flex: 1;
} */

.content .content-right {
  width: 100%;
  height: 100%;
  background: url("./assets/dengimages/e.png") no-repeat;
  background-size: 100% 100%;
  position: relative;
}
.right-infp {
  width: 21.5rem;
  height: 18.5rem;
  margin: auto auto;
  /* border: 1px solid red;*/
  vertical-align: center;
  position: absolute;
  top: 7.2rem;
  left: 4rem;
}
.right-infp-name {
  width: 100%;
  height: 13.5rem;
  background-size: 100% 100%;
  line-height: 1.9rem;
  margin-bottom: 1.8rem;
  position: relative;
}

.kuai {
  width: 100%;
  height: 13.5rem;
}

.kuai video {
  width: 100%;
  height: 13.5rem;
}
.kuai span {
  width: 3.5rem;
  height: 2rem;
}
.kuai .o1 {
  position: absolute;
  border-top: 2px solid aqua;
  border-left: 2px solid aqua;
  left: 25px;
  top: -10px;
}
.kuai .o2 {
  position: absolute;
  border-top: 2px solid aqua;
  border-right: 2px solid aqua;
  right: 25px;
  top: -10px;
}
.kuai .o3 {
  position: absolute;
  border-bottom: 2px solid aqua;
  border-left: 2px solid aqua;
  bottom: -10px;
  left: 25px;
}
.kuai .o4 {
  position: absolute;
  border-bottom: 2px solid aqua;
  border-right: 2px solid aqua;
  bottom: -10px;
  right: 25px;
}
.right-infp-btn {
  width: 100%;

  position: relative;
}
.right-infp-btn a {
  position: relative;
  display: inline-block;
  padding: 10px 20px;
  color: #fff;
  font-size: 16px;
  text-decoration: none;
  text-transform: uppercase;
  overflow: hidden;
  transition: 0.5s;
  border: 2px solid #79bbff;
  background: #79bbff;
  letter-spacing: 4px;
}

.right-infp-btn a:hover {
  background: #00eaff;
  color: #fff;
  border-radius: 5px;
  box-shadow: 0 0 5px #00eaff, 0 0 25px #00eaff, 0 0 50px #00eaff,
    0 0 100px #00eaff;
}

.ml10 {
  margin-left: 10px;
}
</style>




<style lang='scss' scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background: url("@/views/admin/utils/components/FaceRecognition/assets/dengimages/bj.jpg")
    no-repeat;

  background-size: cover;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  border-radius: 6px;

  width: 395px;
  padding: 25px 25px 5px 0;
  .el-input {
    height: 40px;
    input {
      height: 40px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 0px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 40px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 40px;
  padding-left: 12px;
}

//人脸识别登录代码

.loginbox {
  display: flex;
  align-items: center;
  cursor: pointer;
  height: 40px;
  line-height: 40px;
  color: #606266;
  font-size: 14px;
  .el-checkbox {
    margin-bottom: 0 !important;
  }

  .voicebox {
    margin: 0 15px;
  }
}
</style>


<style scoped  lang="scss">
.right-infp-btn {
  display: flex;
  justify-content: space-between;
  a {
    height: 100%;
    width: 49%;
    text-align: center;
    padding: 0;
    font-size: 14px;
    height: 35px;
    line-height: 35px;
    background-color: rgb(64, 158, 255);
    border: none;
  }
}

.voicelogin {
  display: flex;
  align-items: center;
  height: 100%;
  justify-content: center;
  font-size: 16px;
}
</style>